
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Caching;
using KenEgozi.Com.Domain.Aggregates.Common;
using KenEgozi.Com.Domain.Interfaces.Services;
using KenEgozi.Com.Domain.Projections;
using KenEgozi.Com.Domain.Repositories;
using KenEgozi.Com.Weblog.Application;
using NHibernate.Expression;

namespace KenEgozi.Com.Weblog.Services
{
	public class CachedItemsService : ICachedItemsService
	{
		private readonly IBlogProjectionsService blogProjectionsService;

		public CachedItemsService(IBlogProjectionsService blogProjectionsService)
		{
			this.blogProjectionsService = blogProjectionsService;
		}

		static Cache Cache
		{
			get { return HttpContext.Current.Cache; }
		}

		public ICollection<TagCloudEntry> GetTagCloud()
		{
			ICollection<TagCloudEntry> entries = Cache[Keys.Cache.TagCloud] as ICollection<TagCloudEntry>;

			if (entries == null)
			{
				entries = blogProjectionsService.GetTagsData();
				Cache.Insert(Keys.Cache.TagCloud, entries);
			}
			return entries;
		}

		public ICollection<BlogrollEntry> GetBlogroll()
		{
			ICollection<BlogrollEntry> entries = Cache[Keys.Cache.Blogroll] as ICollection<BlogrollEntry>;

			if (entries == null)
			{
				entries = Repository.BlogrollEntries.FindAll(Order.Asc("Title"));
				Cache.Insert(Keys.Cache.Blogroll, entries);
			}
			return entries;
		}

		public ICollection<ArchiveEntry> GetArchive()
		{
			ICollection<ArchiveEntry> entries = Cache[Keys.Cache.Archive] as ICollection<ArchiveEntry>;

			if (entries == null)
			{
				entries = blogProjectionsService.GetArchiveEntries();
				Cache.Insert(Keys.Cache.Archive, entries);
			}
			return entries;
		}

		public ICollection<BadWord> GetBadWords()
		{
			ICollection<BadWord> entries = Cache[typeof(BadWord).Name] as ICollection<BadWord>;

			if (entries == null)
			{
				entries = Repository.BadWords.FindAll();
				Cache.Insert(typeof(BadWord).Name, entries, null, Cache.NoAbsoluteExpiration, new TimeSpan(0,5,0));
			}
			return entries;
		}

		public ICollection<BadIP> GetBadIPs()
		{
			ICollection<BadIP> entries = Cache[typeof(BadIP).Name] as ICollection<BadIP>;

			if (entries == null)
			{
				entries = Repository.BadIPs.FindAll();
				Cache.Insert(typeof(BadIP).Name, entries, null, Cache.NoAbsoluteExpiration, new TimeSpan(0, 5, 0));
			}
			return entries;
		}

		public string AdminPassword
		{
			get
			{
				string adminPassword = Cache[Keys.Cache.AdminPassword] as string;

				if (adminPassword == null)
				{
					adminPassword = Repository.Passwords.Get("Admin").Value;
					Cache.Insert(Keys.Cache.AdminPassword, adminPassword);
				}
				return adminPassword;
			}
		}

		public Stats Stats
		{
			get
			{
				Stats stats = Cache[Keys.Cache.Stats] as Stats;

				if (stats == null)
				{
					stats = blogProjectionsService.GetStats();
					Cache.Insert(Keys.Cache.Stats, stats);
				}
				return stats;
			}
		}

		public void Invalidate(string key)
		{
			Cache.Remove(key);
		}
	}
}
